/*
 * Copyright (C) 2016 Baidu, Inc. All Rights Reserved.
 */
package me.cuilei.springbootdemo.common;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import me.cuilei.springbootdemo.user.User;

/**
 * Created by cuilei05 on 16/10/3.
 */
@Component
public class RedisClient {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Resource(name = "redisTemplate")
    private ValueOperations<String, User> userValueOperations;

    public void add1(final User user) {
        stringRedisTemplate.execute(new RedisCallback<User>() {
            public User doInRedis(RedisConnection connection) throws DataAccessException {
                ObjectMapper mapper = new ObjectMapper();
                RedisSerializer<String> serializer =
                        stringRedisTemplate.getStringSerializer();

                byte[] keySerialized = serializer.serialize("user_" + user.getUserId());
                byte[] valueSerialized = null;
                try {
                    valueSerialized = serializer.serialize(mapper.writeValueAsString(user));
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                connection.set(keySerialized, valueSerialized, Expiration.seconds(10), null);
                return null;
            }
        });
    }

    public User get1(final Long userId) {
        return stringRedisTemplate.execute(new RedisCallback<User>() {
            public User doInRedis(RedisConnection connection) throws DataAccessException {
                ObjectMapper mapper = new ObjectMapper();
                RedisSerializer<String> serializer =
                        stringRedisTemplate.getStringSerializer();

                byte[] keySerialized = serializer.serialize("user_" + userId);
                byte[] valueSerialized = connection.get(keySerialized);

                try {
                    return mapper.readValue(valueSerialized, User.class);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    public void add(User user) {
        userValueOperations.set("user_" + user.getUserId(), user, 20, TimeUnit.SECONDS);
    }

    public User get(Long userId) {
        return userValueOperations.get("user_" + userId);
    }
}
